﻿using AutomaticScrewLocking.domain.torque.dataStorage.model.valobj;
using AutomaticScrewLocking.domain.torque.dataStorage.service;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;


namespace AutomaticScrewLocking.app
{
    public partial class DataQueryForm: Form
    {
        private DataStorageService dataStorageService;
        public DataQueryForm()
        {
            dataStorageService = new DataStorageFactory().getDataStorageService(DataStorageType.MYSQL);
            InitializeComponent();
        }

        private void InitialUi()
        {
            DataQueryChart.Series[0].ChartType = SeriesChartType.Line;
            DataQueryChart.Series[0].Color = Color.LimeGreen;
            DataQueryChart.Series[0].LegendText = "扭力";
            DataQueryChart.Series[0].BorderWidth = 3;
        }

        private void DataQueryForm_Load(object sender, EventArgs e)
        {
            InitialUi();
        }


        private void QueryButton_Click(object sender, EventArgs e)
        {
            query();
        }

        #region 逻辑处理
        private void query()
        {
            DataQueryChart.Series[0].Points.Clear();
            DateTime startTime = StartTimePicker.Value.Date;
            DateTime endTime = EndTimePicker.Value;

            if(startTime > endTime)
            {
                MessageBox.Show("起始时间不能大于结束时间");
            }

            // 查询历史扭力数据
            List<double> torqueValueList = dataStorageService.getHistoryTorqueListByTime(startTime, endTime);
           
           
            if (torqueValueList.Count > 0)
            {
                showUi(torqueValueList);
            }
        }

        private void showUi(List<double> torqueValueList)
        {
            List<double> xList = new List<double>();
            // 生成 X 轴数据：从 0 开始的序号，与扭矩数据的索引一一对应
            for (int i = 0; i < torqueValueList.Count; i++)
            {
                xList.Add(i);
            }
            //  将 X 轴和 Y 轴数据绑定到图表的第一个系列（Series[0]）
            DataQueryChart.Series[0].Points.DataBindXY(xList,torqueValueList);
        }

        #endregion
    }
}
